package com.gexiaobao.pigeon.ui.fragment.match.training

import android.graphics.Typeface
import android.os.Bundle
import android.view.View
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.Constant
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.model.param.SignUpParams
import com.gexiaobao.pigeon.app.util.KvUtils
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentTrainingInfoBinding
import com.gexiaobao.pigeon.ui.activity.ActivityGameResult
import com.gexiaobao.pigeon.viewmodel.TrainingInfoViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 10:56
 * @Describe    : 训放赛 基本信息
 */
class FragmentTrainingInfo : BaseFragment<TrainingInfoViewModel, FragmentTrainingInfoBinding>() {

    private var mId: Int = 0
    private var raceId: Int = 0
    private var title: String = ""
    private var signUpStatus = 0
    private var passStatus = 0
    private var mFollowStatus = 0
    private var memberCode = ""//获取棚号

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mId = requireArguments().getInt("id")
        raceId = requireArguments().getInt("raceId")
        mFollowStatus = requireArguments().getInt("followStatus")
        passStatus = requireArguments().getInt("passStatus")
        title = arguments?.getString("title").toString() + getString(R.string.kilometre)
        title.let {
            mDatabind.includeBar.toolbar.initClose(it) {
                nav().navigateUp()
            }
        }
    }

    override fun initData() {
        super.initData()
        mDatabind.tvJiePigeonDetail.setTypeface(Typeface.SANS_SERIF, Typeface.ITALIC)
        mViewModel.signUpContent.set(getString(R.string.go_to_sign))
        mDatabind.swipeRefreshView.init {
            showLoading(getString(R.string.loading))
            mViewModel.getPassDetailApp(mId.toString())
            mViewModel.getMemberNum(mId.toString())
        }
    }

    override fun onResume() {
        super.onResume()
        showLoading(getString(R.string.loading))
        mViewModel.getMemberNum(mId.toString())
        mViewModel.getPassDetailApp(mId.toString())
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.tvTrainingInfoGoSignUp,
            mDatabind.llTrainingSignUpDetail,
            mDatabind.raceDetailGeneralRaceResult,
            mDatabind.raceDetailGeneralCarMap,
            mDatabind.raceDetailGeneralFlyedMap,
            mDatabind.tvJiePigeonDetail
        ) {
            when (it) {
                mDatabind.llTrainingSignUpDetail -> {
                    nav().navigateAction(R.id.action_to_training_sign_up_detail, Bundle().apply {
                        putInt("id", mId)
                        putString("title", title)
                        putString("memberCode", memberCode)
                    })
                }

                mDatabind.tvTrainingInfoGoSignUp -> {
                    if (mViewModel.signUpContent.get() == "取消报名") {
                        val userId = KvUtils.decodeString(Constant.USERID).toInt()
                        val params = SignUpParams(
                            passId = mId,
                            userId = userId,
                            pIds = arrayListOf()
                        )
                        showLoading(getString(R.string.loading))
                        mViewModel.cancelTraining(params)
                    } else {
                        nav().navigateAction(R.id.action_to_training_sign_up, Bundle().apply {
                            putInt("signUpStatus", signUpStatus)
                            putString("title", title)
                            putString("type", "auto")
                            putString("passId", mId.toString())
                        })
                    }
                }

                mDatabind.raceDetailGeneralRaceResult -> {
                    startActivity<ActivityGameResult>(
                        "title" to title,
                        "passId" to mId,
                        "type" to "training"
                    )
                    activity?.overridePendingTransition(
                        android.R.anim.fade_in,
                        android.R.anim.fade_out
                    )
                }

                mDatabind.raceDetailGeneralCarMap -> {
                    nav().navigateAction(R.id.action_to_car_vehicle_route, Bundle().apply {
                        putString("title", title)
                        putInt("id", mId)
                    })
                }

                mDatabind.raceDetailGeneralFlyedMap -> {
                    if (passStatus < 6) {
                        showMessage(getString(R.string.no_fly_and_wait_look))
                        return@setOnclickNoRepeat
                    }
                    nav().navigateAction(R.id.action_to_race_training_map, Bundle().apply {
                        putInt("mId", mId)
                        putString("title", title)
                    })
                }

                mDatabind.tvJiePigeonDetail -> {
                    nav().navigateAction(R.id.action_to_pick_up_pigeon_detail, Bundle().apply {
                        putString("title", title)
                        putInt("raceId", raceId)
                    })
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.detailInfoResult.observe(viewLifecycleOwner) {
            dismissLoading()
            if (mDatabind.swipeRefreshView.isRefreshing) {
                mDatabind.swipeRefreshView.isRefreshing = false
            }
            mViewModel.trainingSignUpPigeonTime.set(it.loadTime)
            mViewModel.trainingTime.set(it.flyTime)
            mViewModel.trainingPlace.set(it.flyPlace)
            mViewModel.flyDissance.set(it.flyDis)
            mViewModel.trainingLon.set(it.flyLonShow)
            mViewModel.trainingLat.set(it.flyLatShow)
            mViewModel.trainingWeather.set(it.weather)
            mViewModel.trainingTemperature.set(it.temperature)
            mViewModel.trainingWindPower.set(it.wind.trim())
            mViewModel.trainingSignUpNo.set(it.signNum)
            mViewModel.trainingIntoNo.set(it.loadedNum)
            mViewModel.trainingBackNo.set(it.backNum)
            signUpStatus = it.signStatus
            if (mFollowStatus != 1) {
                mDatabind.tvTrainingInfoGoSignUp.visibility = View.GONE
            } else {
                when (signUpStatus) {
                    1 -> {
                        mDatabind.tvTrainingInfoGoSignUp.visibility = View.VISIBLE
                        mViewModel.signUpContent.set(getString(R.string.go_to_sign))
                    }

                    2 -> {
                        mDatabind.tvTrainingInfoGoSignUp.visibility = View.VISIBLE
                        mViewModel.signUpContent.set(getString(R.string.cancel_registration))
                    }

                    else -> {
                        mDatabind.tvTrainingInfoGoSignUp.visibility = View.GONE
                    }
                }
            }
        }

        mViewModel.cancelTrainingResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                mViewModel.getPassDetailApp(mId.toString())
                RxToast.showToast("已" + getString(R.string.cancel_registration))
                mViewModel.signUpContent.set(getString(R.string.go_to_sign))
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }

        mViewModel.memberNumResult.observe(viewLifecycleOwner) {
            if (it != null) {
                memberCode = it.memberNum
            }
        }
    }
}